﻿using mediaControl.Vo;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace mediaControl.ViewModels
{
    class ImagePlayerViewModel : BindableBase
    {
        private readonly IEventAggregator _eventAggregator;

        private List<ProgramItem> currentProgramList;

        private string cycleModel;
        public ImagePlayerViewModel(DialogParameters parameters, IEventAggregator eventAggregator)
        {
            currentProgramList = new List<ProgramItem>();
            List<PlaySourceVo> playSourceVos = parameters.GetValue<List<PlaySourceVo>>("source");
            cycleModel = parameters.GetValue<string>("cycleModel");
            imagePaths = new List<string>();
            foreach (PlaySourceVo playSourceVo in playSourceVos)
            {
                currentProgramList.Add(playSourceVo.SouceItem);
                if (Directory.Exists(playSourceVo.SouceItem.ItemPath))
                {
                    //文件夹
                    imagePaths.AddRange(getImageList(playSourceVo.SouceItem.ItemPath));
                }
                else
                {
                    imagePaths.Add(playSourceVo.SouceItem.ItemPath);
                }
            }


            //图片
            if (null != imagePaths && imagePaths.Count > 0)
            {
                timer.Interval = TimeSpan.FromSeconds(5);
                timer.Tick += Timer_Tick;
                ImageSrc = imagePaths[0];
                timer.Start();
            }
            _eventAggregator = eventAggregator;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            index++;
            if (index >= imagePaths.Count)
            {
                index = 0;
            }
            ImageSrc = imagePaths[index];
        }

        private string imageSrc;

        private int index = 0;

        private List<string> imagePaths;

        private DispatcherTimer timer = new DispatcherTimer();

        public string Title { get; set; }
        public string ImageSrc { get { return imageSrc; } set { this.imageSrc = value; RaisePropertyChanged(); } }

        public bool IsImageFile(string filePath)
        {
            try
            {
                using (var image = new Bitmap(filePath))
                    return true;
            }
            catch (Exception ex) when (ex is ArgumentNullException || ex is NotSupportedException)
            {
                return false;
            }
        }

        public List<string> getImageList(string folderPath)
        {

            List<String> imageList = new List<string>();
            try
            {
                // 获取文件夹中所有文件的列表
                string[] files = Directory.GetFiles(folderPath);

                foreach (string file in files)
                {
                    if (IsImageFile(file))
                    {
                        imageList.Add(file);
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error reading folder content: " + ex.Message);
            }
            return imageList;
        }
    }
}
